﻿// ==UserScript==
// @name           QuickSwitch.uc.js
// @description   一键快速切换UI和主题
// @author         Runningcheese
// @namespace   http://www.runningcheese.com
// @include        main
// @license         MIT License
// @compatibility  Firefox 29+
// @charset        UTF-8
// @version        v2017.02.07 
// @version        v2016.01.05 
// @homepage    http://www.runningcheese.com/firefox-v8
// ==/UserScript==

//载入脚本
function jsonToDOM(json, doc, nodes) {

    var namespaces = {
        html: 'http://www.w3.org/1999/xhtml',
        xul: 'http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul'
    };
    var defaultNamespace = namespaces.html;

    function namespace(name) {
        var m = /^(?:(.*):)?(.*)$/.exec(name);        
        return [namespaces[m[1]], m[2]];
    }

    function tag(name, attr) {
        if (Array.isArray(name)) {
            var frag = doc.createDocumentFragment();
            Array.forEach(arguments, function (arg) {
                if (!Array.isArray(arg[0]))
                    frag.appendChild(tag.apply(null, arg));
                else
                    arg.forEach(function (arg) {
                        frag.appendChild(tag.apply(null, arg));
                    });
            });
            return frag;
        }

        var args = Array.slice(arguments, 2);
        var vals = namespace(name);
        var elem = doc.createElementNS(vals[0] || defaultNamespace, vals[1]);

        for (var key in attr) {
            var val = attr[key];
            if (nodes && key == 'id')
                nodes[val] = elem;

            vals = namespace(key);
            if (typeof val == 'function')
                elem.addEventListener(key.replace(/^on/, ''), val, false);
            else
                elem.setAttributeNS(vals[0] || '', vals[1], val);
        }
        args.forEach(function(e) {
            try {
                elem.appendChild(
                                    Object.prototype.toString.call(e) == '[object Array]'
                                    ?
                                        tag.apply(null, e)
                                    :
                                        e instanceof doc.defaultView.Node
                                        ?
                                            e
                                        :
                                            doc.createTextNode(e)
                                );
            } catch (ex) {
                elem.appendChild(doc.createTextNode(ex));
            }
        });
        return elem;
    }
    return tag.apply(null, json);
}


//定义按钮
CustomizableUI.createWidget({
    id: 'QuickSwitch',
    defaultArea: CustomizableUI.AREA_NAVBAR,
    label: '主题',
    tooltiptext: '主题切换',
    onCreated: function(aNode) {
     aNode.setAttribute('type', 'menu');

        
 //定义菜单      
        var myMenuJson = 
                                ['xul:menupopup', {id: 'QuickSwitch_pop'},
                                ['xul:menuitem', {label: '主题: Sports',oncommand: 'event.stopPropagation(); QuickSwitch_Sports();',class:'menuitem-iconic', image:''}],
                                ['xul:menuitem', {label: '极简: Sports',oncommand: 'event.stopPropagation(); QuickSwitch_Tabontop_Sports();',class:'menuitem-iconic', image:''}],
                                ['xul:menuitem', {label: '主题: Yosemite',oncommand: 'event.stopPropagation(); QuickSwitch_Yosemite();',class:'menuitem-iconic', image:''}],
                                ['xul:menuitem', {label: '极简: Yosemite',oncommand: 'event.stopPropagation(); QuickSwitch_Tabontop_Yosemite();',class:'menuitem-iconic', image:''}],
                                ['xul:menuitem', {label: '主题: Nightly',oncommand: 'event.stopPropagation(); QuickSwitch_Nightly();',class:'menuitem-iconic', image:''}],
                                ['xul:menuitem', {label: '极简: Nightly',oncommand: 'event.stopPropagation(); QuickSwitch_Tabontop_Nightly();',class:'menuitem-iconic', image:''}],
                                ['xul:menuitem', {label: '主题: 原生主题',oncommand: 'event.stopPropagation(); QuickSwitch_Aero();',class:'menuitem-iconic', image:''}],
                                ['xul:menuitem', {label: '壁纸: 更换壁纸', tooltiptext: '右键点击菜单不关闭',onclick: 'event.stopPropagation(); event.preventDefault(); RandomIMG();',class:'menuitem-iconic', image:''}],
                                ['xul:menuitem', {label: '壁纸: 停用壁纸',onclick: 'event.stopPropagation(); event.preventDefault(); StopUpdateIMG();',class:'menuitem-iconic', image:''}],
                                ['xul:menuseparator', {}],
                                ['xul:menuitem', {label: '上传壁纸？',oncommand: 'event.stopPropagation(); var x = gBrowser.mCurrentTab._tPos + 1; gBrowser.moveTabTo(gBrowser.selectedTab =gBrowser.addTab("http://www.runningcheese.com/wallpaper"), x);',class:'menuitem-iconic', image:''}]
                                ];
        aNode.appendChild(jsonToDOM(myMenuJson, aNode.ownerDocument, {}));
        aNode.setAttribute('menupopup', 'QuickSwitch_pop');
    }
});


//定义图标
var cssStr = '@-moz-document url("chrome://browser/content/browser.xul"){'
		 + '#QuickSwitch[cui-areatype="toolbar"] .toolbarbutton-icon {'
		 + 'list-style-image:url()'
		 + '}}'
     + '#QuickSwitch[cui-areatype="menu-panel"] .toolbarbutton-icon, toolbarpaletteitem[place="palette"]> #QuickSwitch .toolbarbutton-icon {'
		 + 'list-style-image:url()'
		 + '}}';
	var sss = Cc["@mozilla.org/content/style-sheet-service;1"].getService(Ci.nsIStyleSheetService);
	var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
	sss.loadAndRegisterSheet(ios.newURI("data:text/css;base64," + btoa(cssStr), null, null), sss.USER_SHEET);




//定义函数
 function	QuickSwitch_Sports() {
        var id = [1,9]
        var service = Components.classes["@userstyles.org/style;1"].getService(Components.interfaces.stylishStyle)
        for (var i=0; i < id.length; i++){
            var style = service.find(id[i], service.REGISTER_STYLE_ON_CHANGE);
            style.enabled = 1;
            style.save();
        };
        var id = [2,3,4,7,8]
        var service = Components.classes["@userstyles.org/style;1"].getService(Components.interfaces.stylishStyle)
        for (var i=0; i < id.length; i++){
            var style = service.find(id[i], service.REGISTER_STYLE_ON_CHANGE);
            style.enabled = 0;
            style.save();
        };
};


function	QuickSwitch_Yosemite() {
        var id = [2,9]
        var service = Components.classes["@userstyles.org/style;1"].getService(Components.interfaces.stylishStyle)
        for (var i=0; i < id.length; i++){
            var style = service.find(id[i], service.REGISTER_STYLE_ON_CHANGE);
            style.enabled = 1;
            style.save();
        };
        var id = [1,3,4,7,8]
        var service = Components.classes["@userstyles.org/style;1"].getService(Components.interfaces.stylishStyle)
        for (var i=0; i < id.length; i++){
            var style = service.find(id[i], service.REGISTER_STYLE_ON_CHANGE);
            style.enabled = 0;
            style.save();
        };
};


function	QuickSwitch_Nightly() {
        var id = [3,9]
        var service = Components.classes["@userstyles.org/style;1"].getService(Components.interfaces.stylishStyle)
        for (var i=0; i < id.length; i++){
            var style = service.find(id[i], service.REGISTER_STYLE_ON_CHANGE);
            style.enabled = 1;
            style.save();
        };
        var id = [1,2,4,7,8]
        var service = Components.classes["@userstyles.org/style;1"].getService(Components.interfaces.stylishStyle)
        for (var i=0; i < id.length; i++){
            var style = service.find(id[i], service.REGISTER_STYLE_ON_CHANGE);
            style.enabled = 0;
            style.save();
        };
};


function	QuickSwitch_Aero() {
        var id = [4,9]
        var service = Components.classes["@userstyles.org/style;1"].getService(Components.interfaces.stylishStyle)
        for (var i=0; i < id.length; i++){
            var style = service.find(id[i], service.REGISTER_STYLE_ON_CHANGE);
            style.enabled = 1;
            style.save();
        };
        var id = [1,2,3,7,8]
        var service = Components.classes["@userstyles.org/style;1"].getService(Components.interfaces.stylishStyle)
        for (var i=0; i < id.length; i++){
            var style = service.find(id[i], service.REGISTER_STYLE_ON_CHANGE);
            style.enabled = 0;
            style.save();
        };
};


function	QuickSwitch_Tabontop_Sports() {
        var id = [1,7,9]
        var service = Components.classes["@userstyles.org/style;1"].getService(Components.interfaces.stylishStyle)
        for (var i=0; i < id.length; i++){
            var style = service.find(id[i], service.REGISTER_STYLE_ON_CHANGE);
            style.enabled = 1;
            style.save();
        };
        var id = [2,3,4,8]
        var service = Components.classes["@userstyles.org/style;1"].getService(Components.interfaces.stylishStyle)
        for (var i=0; i < id.length; i++){
            var style = service.find(id[i], service.REGISTER_STYLE_ON_CHANGE);
            style.enabled = 0;
            style.save();
        };
};


function	QuickSwitch_Tabontop_Yosemite() {
        var id = [2,8,9]
        var service = Components.classes["@userstyles.org/style;1"].getService(Components.interfaces.stylishStyle)
        for (var i=0; i < id.length; i++){
            var style = service.find(id[i], service.REGISTER_STYLE_ON_CHANGE);
            style.enabled = 1;
            style.save();
        };
        var id = [1,3,4,7]
        var service = Components.classes["@userstyles.org/style;1"].getService(Components.interfaces.stylishStyle)
        for (var i=0; i < id.length; i++){
            var style = service.find(id[i], service.REGISTER_STYLE_ON_CHANGE);
            style.enabled = 0;
            style.save();
        };
};

function	QuickSwitch_Tabontop_Nightly() {
        var id = [3,8,9]
        var service = Components.classes["@userstyles.org/style;1"].getService(Components.interfaces.stylishStyle)
        for (var i=0; i < id.length; i++){
            var style = service.find(id[i], service.REGISTER_STYLE_ON_CHANGE);
            style.enabled = 1;
            style.save();
        };
        var id = [1,2,4,7]
        var service = Components.classes["@userstyles.org/style;1"].getService(Components.interfaces.stylishStyle)
        for (var i=0; i < id.length; i++){
            var style = service.find(id[i], service.REGISTER_STYLE_ON_CHANGE);
            style.enabled = 0;
            style.save();
        };
};


function	RandomIMG() {
(function(){
        var id = [14]
        var service = Components.classes["@userstyles.org/style;1"].getService(Components.interfaces.stylishStyle)
        for (var i=0; i < id.length; i++){
            var style = service.find(id[i], service.REGISTER_STYLE_ON_CHANGE);
            style.enabled = 0;
            style.save();
        }
    })();
document.getElementById("Browser:Reload").doCommand();
(function(){
        var id = [14]
        var service = Components.classes["@userstyles.org/style;1"].getService(Components.interfaces.stylishStyle)
        for (var i=0; i < id.length; i++){
            var style = service.find(id[i], service.REGISTER_STYLE_ON_CHANGE);
            style.enabled = 1;
            style.save();
        }
    })();

};


function	StopUpdateIMG() {
(function(){
        var id = [14]
        var service = Components.classes["@userstyles.org/style;1"].getService(Components.interfaces.stylishStyle)
        for (var i=0; i < id.length; i++){
            var style = service.find(id[i], service.REGISTER_STYLE_ON_CHANGE);
            style.enabled = 0;
            style.save();
        }
    })();
};


